<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   zhaozilong
 * @Last Modified time: 2020-11-09 15:46:28
 */

namespace app\admin\controller;
use app\common\controller\Base;
use think\captcha\Captcha;
use think\Url;

class Login extends Base {
	function initialize() {
		parent::initialize();

		$this->adminUserLogic = logic('AdminUser');
	}

	public function index() {
		return $this->fetch();
	}

	public function doLogin() {
		$username = input('post.username', '', 'pwEscape');
		$password = input('post.password', '', 'pwEscape');
		$rememberme = input('post.rememberme', '', 'intval');
		$data = [
			'username' => $username,
			'password' => $password,
		];
		$result = $this->validate($data,[
			'username' => 'require|min:1',
			'password' => 'require|length:5,32',
			//['captcha','require','请填写验证码']
		],[
			'username.require' => '登录名不能为空',
			'username.min' => '登录名格式不正确',
			'password.require' => '请填写密码',
			'password.length' => '密码格式不正确',
		]);
		if (true !== $result) {
			// 验证失败 输出错误信息
			$this->error($result);
			exit;
		}

		// $captcha = new Captcha();
		// if(!$captcha->check($data['captcha'],1)){
		// 	$this->error('验证码错误');
		// }

		$result = $this->adminUserLogic->login($data['username'], $data['password'], $rememberme);
		//print_r($result);exit;

		if ($result['code']==1) {
			$uid = !empty($result['data']['uid']) ? $result['data']['uid']:0;
			$this->success('登录成功', url('admin/index/index'));
		} elseif ($result['code']==0) {
			$this->error($result['msg']);
		} else {
			$this->logout();
		}
	}

	
	/**
	 * 退出登录
	 * @return [type] [description]
	 */
	public function logout() {
		$this->adminUserLogic->logout();

		$this->redirect('admin/login/index');
	}

	//图片验证码
	public function verify_img($id = 1){
		$captcha = new Captcha((array)config('captcha'));
		return $captcha->entry($id);
	}

}